const app = getApp()
Page({
  data: {
    courseTitle:'',
    courseCode:'',
    sectionId:'',
    menuList:[]

  },

  widgetsToggle: function (e) {
    var id = e.currentTarget.id, menuList = this.data.menuList;
      for (var i = 0, len = menuList.length; i < len; ++i) {
      if (menuList[i].chaptId == id) {
        menuList[i].open = !menuList[i].open;
      } else {
        menuList[i].open = false;
      }
    }
    this.setData({
      menuList:menuList,

    });
   
  },

/**
  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.target.dataset.urls
    })
  },
 */

  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab == e.target.dataset.current) {
      return false;

    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

//获到菜单标题文字
  getMenu:function(courseCode){
    var that = this
    var p = new Promise(function (resolve, reject) {
      wx.request({
          url: 'https://www.beicho.com/get_courseMenu.php',  //接口地址 
          method: 'GET',
          header: {
             'content-type': 'application/json'
          },
          data: {
             courseCode:courseCode
          },
          success: res => {
          //console.log('home数据库获到的章节菜单:', res.data)
          //console.log('home数据库获到的章节菜单:', res.data.chaptMenu)          
          that.setData({
            menuList: res.data.chaptMenu     
          })

          //成功执行之后，返回
          resolve('success')
        }

      })

    })   //promise
    return p
   },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
       courseCode: options.courseCode,
       courseTitle:options.courseTitle

    })  //必须放在onLoad中，因为onShow靠后
        //console.log("home页面跳转拿到的_综合courseCode:", this.data.courseCode)
    
    //此数据用于判断是否有授权
    app.globalData.selectCourse = options.courseCode

    var that=this
    that.getMenu(options.courseCode).then(function (p) {
     // console.log(p);
      //后面可以用传过来的数据做些其他操作
      //console.log('取出来的数据:', that.data.menuList)
      // console.log('节标题:',that.data.menuList[0].sectionRec[0].sectionTitle)

    });
  
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})
